/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.DonDatHang_dao;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.DonDatHang_pojo;
import pojo.KhachHang_pojo;

/**
 *
 * @author Rocker000
 */
public class dondathang_controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            HttpSession session = request.getSession();
            String action = request.getParameter("action");
            if(action.equals("mua"))
            {
                out.println("1");
                //THÊM ĐƠN ĐẶT HÀNG
                String tenkhachhang="";
                String username="";
                String email="";
                String diachi="";
                String sodienthoai="";
                String masanpham=request.getParameter("masanpham");
                //int soluong = Integer.parseInt(String.valueOf(request.getParameter("soluong")));
                out.println("2");
                //Lay gio he thong
                Date todayD=new Date(System.currentTimeMillis()); 

                //Khai bao dinh dang ngay thang
                SimpleDateFormat dayFormat= new SimpleDateFormat("dd/MM/yyyy"); 

                //parse ngay thang sang dinh dang va chuyen thanh string.
                String ngaydathang=dayFormat.format(todayD.getTime());
                out.println("3");
                out.println("abc");
                tenkhachhang = new String(request.getParameter("txthoten").getBytes("ISO8859_1"),"UTF8");
                email= new String(request.getParameter("txtemail").getBytes("ISO8859_1"),"UTF8");
                diachi = new String(request.getParameter("txtdiachi").getBytes("ISO8859_1"),"UTF8");
                sodienthoai= request.getParameter("txtsodienthoai"); 
                
                if(session.getAttribute("KhachHang")!=null)
                {
                    KhachHang_pojo kh = (KhachHang_pojo)session.getAttribute("KhachHang");
                    username= kh.getTenDangNhap();
                }
                out.println("4");
                DonDatHang_pojo dondathang = new DonDatHang_pojo();
                dondathang.setDiachi(diachi);
                dondathang.setEmail(email);
                dondathang.setNgaydathang(ngaydathang);
                dondathang.setSodienthoai(sodienthoai);
                dondathang.setTendangnhap(username);
                dondathang.setTenkhachhang(tenkhachhang);
                dondathang.setSoluong(1);
                dondathang.setMasanpham(masanpham); 
                DonDatHang_dao.ThemDonDatHang(dondathang);
                
                //int madondathang = DonDatHang_dao.LayMa(email, sodienthoai, ngaynhap);
                //out.println(madondathang);
                //THÊM CHI TIẾT ĐỢN ĐẶT HÀNG
                
                response.sendRedirect("Home.jsp");
                
            }
            
            if(action.equals("xacnhandathanhtoan"))
            {
                int madondathang = Integer.parseInt(request.getParameter("madondathang"));
                DonDatHang_dao.XacNhanDaThanhToan(madondathang);
                response.sendRedirect("dondathang.jsp");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
